package com.hwadee.mapper;

import com.hwadee.pojo.Device;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DeviceMapper {
    //根据用户名和设备编号模糊查询设备列表
    @Select("<script>" +
            "SELECT * FROM device WHERE 1=1 " +
            "<if test='username != null and username != \"\"'> AND username LIKE CONCAT('%', #{username}, '%') </if>" +
            "<if test='sn != null and sn != \"\"'> AND sn LIKE CONCAT('%', #{sn}, '%') </if>" +//如果 username 或 sn 为空，则忽略对应的过滤条件
            "</script>")
    List<Device> searchDevice(@Param("username") String username, @Param("sn") String sn);
    //根据设备 ID 删除设备
    @Delete("DELETE FROM device WHERE id = #{id}")
    void deleteById(@Param("id") Integer id);
    //根据设备 ID 更新设备信息
    @Update("UPDATE device SET username=#{username}, sn=#{sn}, status=#{status} WHERE id=#{id}")
    void update(Device device);
    //插入新设备记录
    @Insert("INSERT INTO device(username, sn, status) VALUES(#{username}, #{sn}, #{status})")
    void insert(Device device);
}
